import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 标本拆分状态
		splitStatus: [{ name:'已拆分', value:'1' },{ name:'待拆分', value:'0' },{ name:'禁止拆分', value:'-1' }],
		// 搜索表单列表
		forms : [
			{ label: '原条码',		name: 'old_spec_code' },
			{ label: '标本架',		name: 'sf_name' },
			{ label: '标本架代码',	name: 'sf_code' },
			{ label: '科室',		name: 'project_parent_id',			type:'select',		loading:true,	idStr:'uuid',	onValue:data=>$fn.onKeShi.call(this,data,{})},
			{ label: '岗位',		name: 'project_id',					type:'select', 		loading:true},
			{ label: '创建时间',		names: ['start_date','end_date'], 	type:'date-range', 	loading:true },
		],
	}
	query = { split_status: 0 }
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 科室
			$fn.setRow( forms, 'project_parent_id', await f.getCache({ api:'ke_shi' }) )
			// 岗位
			this.resetData = await f.getCache({ api:'gang_wei' })
			$fn.setRow( forms, 'project_id', this.resetData )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'spec-split/index')
	
	// 表单
	cols = [
		{ title: '原条码', 		dataIndex: 'old_spec_code', 		...$fn.table.spec_code },
		{ title: '新条码', 		dataIndex: 'new_spec_code', 		...$fn.table.spec_code },
		{ title: '标本架名称', 	dataIndex: 'sf_name', 				width:150 },
		{ title: '标本架代码', 	dataIndex: 'sf_code', 				...$fn.table.exp_num },
		{ title: '标本架编号', 	dataIndex: 'sf_number', 			...$fn.table.exp_num },
		{ title: '科室', 		dataIndex: 'parent_project_name', 	width:150 },
		{ title: '岗位', 		dataIndex: 'project_name', 			width:150 },
		{ title: '项目名称', 	dataIndex: 'kind_str', 				width:280 },
		{ title: '创建时间', 	dataIndex: 'created_at',  			...$fn.table.time},
		{ title: '状态', 		dataIndex: 'split_status', 			width:100,		render: text => $fn.getState(this.state.splitStatus,text) },
	]
	
	// 操作按钮
	controls = () => {
		const { selectedRows } = this.state
		let spec_code = selectedRows.map(item => item.new_spec_code)
		return [
			Confirm.call(this, '批量拆分完成', 'spec-split/split', { query: {spec_code} , isKeys:1, code: 'F1' }),		// 批量拆分完成
			Controls.printer.call(this, {label:'打印',code: 50103, batch:true,} )							// 打印
		]
	}
	
	render(){
		return (
			<Page title='待拆分'  controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this, {resetField: 'project_id'}) }
				{/* 表格 */}
				{ Table.call(this, {isInit: window.isInit} ) }
			</Page>
		)
	}
}

export default Index